/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.istia.gesstages.actions;

import fr.istia.gesstages.metier.User;
import fr.istia.gesstages.metier.UserType;
import fr.istia.gesstages.util.Encryption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.commons.beanutils.BeanComparator;

public class UserAction extends ParentAction {

    // gestion des user
    private List<User> usersList = new ArrayList<User>();
    private User user;
    private String login;
    private String mdp = "";
    private String mdpvalidate = "";
    private String titleEditPage = "";
    // gestion des userType
    private List<UserType> userTypes = new ArrayList<UserType>();
    private int typeid;

    public List<User> getUsersList() {
        return usersList;
    }

    public void setUsersList(List<User> usersList) {
        this.usersList = usersList;
    }

    public String getMdp() {
        return mdp;
    }

    public void setMdp(String mdp) {
        this.mdp = mdp;
    }

    public String getMdpvalidate() {
        return mdpvalidate;
    }

    public void setMdpvalidate(String mdpvalidate) {
        this.mdpvalidate = mdpvalidate;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getTitleEditPage() {
        return titleEditPage;
    }

    public void setTitleEditPage(String titleEditPage) {
        this.titleEditPage = titleEditPage;
    }

    public List<UserType> getUserTypes() {
        userTypes = metier.findAllUserTypes();
        Collections.sort(userTypes, new BeanComparator("id"));
        return userTypes;
    }

    public void setUserTypes(List<UserType> userTypes) {
        this.userTypes = userTypes;
    }

    public int getTypeid() {
        return typeid;
    }

    public void setTypeid(int typeid) {
        this.typeid = typeid;
    }

    public String updateUsersList() {
        usersList = metier.findAllUsers();
        user=null;
        for(User u : usersList){
            if(u.getId().equals(((User) session.get("user")).getId()))
                    user=u;
        }
        if(user!=null)
            usersList.remove(user);
        Collections.sort(usersList, new BeanComparator("id"));
        return SUCCESS;
    }
    public String findProfil() {
        try {
            user = (User) session.get("user");
            titleEditPage = getText("profil.admin.title");
        } catch (Exception ex) {
            addActionError(getText("error.user.find") + " : " + ex.toString());
            return ERROR;
        }
        return SUCCESS;
    }

    public String findUser() {
        try {
            user = metier.findUserByLogin(login);
            titleEditPage = getText("profil.user.title") + user.getPrenom() + " " + user.getNom();
        } catch (Exception ex) {
            addActionError(getText("error.user.find") + " : " + ex.toString());
            return ERROR;
        }
        return SUCCESS;
    }

    public String validateEditProfil() {
        // vérification du nouveau mot de passe
        if (!mdp.equals("")) {
            if (mdp.equals(mdpvalidate)) {
                user.setMdp(Encryption.md5(mdp));
            } else {
                addActionError(getText("error.user.password"));
                return INPUT;
            }
        }

        // mise à jour
        try {
            user.setType(metier.findUserByLogin(user.getLogin()).getType());
            if(this.typeid!=0)
                user.setType(metier.findUserTypeById(typeid));
            metier.updateUser(user);
            // si le profil est celui de l'utilisateur
            if (user.getLogin().equals(((User) session.get("user")).getLogin())) {
                session.put("user", user);
            } else {
                return SUCCESS; // profil d'un utilisateur autre
            }
        } catch (Exception ex) {
            addActionError(getText("error.user.update") + " : " + ex.toString());
            return ERROR;
        }
        addActionMessage(getText("user.edit.ok"));
        return INPUT;
    }

    public String validateUser() {
        // vérification du nouveau mot de passe
        if (!mdp.equals("")) {
            if (mdp.equals(mdpvalidate)) {
                user.setMdp(Encryption.md5(mdp));
            } else {
                addActionError(getText("error.user.password"));
                return INPUT;
            }
        }

        // mise à jour
        try {
            user.setType(metier.findUserTypeById(typeid));
            user.setLogin(user.getPrenom() + "." + user.getNom());
            metier.createUser(user);
        } catch (Exception ex) {
            addActionError(getText("error.user.create") + " : " + ex.toString());
            return ERROR;
        }
        return SUCCESS;
    }

    public String deleteUser() {
        try {
            user = metier.findUserByLogin(login);
            user.setType(metier.findUserByLogin(user.getLogin()).getType());
            metier.deleteUser(user);
        } catch (Exception ex) {
            addActionError(getText("error.user.delete") + " : " + ex.toString());
            return ERROR;
        }
        return SUCCESS;
    }
}
